package com.ziven.common.widget.value;

import android.view.View;

import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;

import com.ziven.common.widget.WidgetUtils;
import com.ziven.common.widget.title.TitleView;
import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public class TitleSize extends Value {

    private boolean isSubTitle = false;

    public static TitleSize dimenId(@DimenRes int dimenId) {
        return new TitleSize().accept(my -> my.resId = dimenId);
    }

    public static TitleSize size(float size) {
        return new TitleSize().accept(my -> my.value = size);
    }

    public static TitleSize size(@NonNull String sp) {
        return size(WidgetUtils.dimen(sp));
    }

    public static TitleSize subDimenId(@DimenRes int dimenId) {
        return new TitleSize().accept(my -> {
            my.resId = dimenId;
            my.isSubTitle = true;
        });
    }

    public static TitleSize subSize(float size) {
        return new TitleSize().accept(my -> {
            my.value = size;
            my.isSubTitle = true;
        });
    }

    public static TitleSize subSize(@NonNull String sp) {
        return subSize(WidgetUtils.dimen(sp));
    }

    private TitleSize() {}

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {
        helper.getViewNulls().doNotNull(view -> {
            if(view instanceof TitleView) {
                if(resId == 0) {
                    if(isSubTitle) {
                        value(Float.class, value -> ((TitleView) view).setSubtitleSize(value));
                    } else {
                        value(Float.class, value -> ((TitleView) view).setTitleSize(value));
                    }
                } else {
                    if(isSubTitle) {
                        ((TitleView) view).setSubtitleSizeId(resId);
                    } else {
                        ((TitleView) view).setTitleSizeId(resId);
                    }
                }
            }
        });
    }
}
